package com.jeffpalm.floto.isight.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Misc utility methods.
 */
public final class Util {

  private Util() {}

  /**
   * Returns the entire input stream, minus any trailing newlines.
   * 
   * @returns a {@link java.lang.String} from reading from the entire
   *          <code>ins</code>minus any trailing newlines.
   */
  public static String readAndClose(InputStream ins) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(ins));
    StringBuffer result = new StringBuffer();
    String line;
    while ((line = in.readLine()) != null) {
      result.append(line).append("\n");
    }
    in.close();
    return result.toString().trim();
  }

  /**
   * Returns <code>true</code> if <code>s</code> is empty or
   * <code>null</code>.
   * 
   * @param s
   *          String in question
   * @return <code>true</code> if <code>s</code> is empty or
   *         <code>null</code>.
   */
  public static boolean isEmpty(String s) {
    return s == null || s.equals("");
  }

  /**
   * Retuns the answer to <code>question</code> on STDIN after prompting the
   * user.
   * 
   * @param question
   *          the prompt to the user
   * @return the answer to <code>question</code> on STDIN after prompting the
   *         user.
   * @throws IOException
   */
  public static String prompt(String question) throws IOException {
    String line = null;
    while (true) {
      System.out.print(question + "> ");
      System.out.flush();
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      line = in.readLine();
      if (!isEmpty(line)) break;
    }
    return line;
  }

  /**
   * Returns the trimmed String or <code>null</code> if <code>s</code> is
   * <code>null</code>.
   * 
   * @param s
   *          subject
   * @return the trimmed String or <code>null</code> if <code>s</code> is
   *         <code>null</code>
   */
  /**
   * @param s
   * @return
   */
  public static String trim(String s) {
    return s == null ? null : s.trim();
  }

  /**
   * Returns a new String from <code>s</code> and a newline.
   * 
   * @param s
   *          subject
   * @return a new String from <code>s</code> and a newline.
   */
  public static String newline(Object s) {
    StringBuffer sb = new StringBuffer(String.valueOf(s));
    sb.append("\n");
    return sb.toString();

  }

  /**
   * Asynchronously executes the <code>runner</code>.
   * 
   * @param runner
   *          {@link Runnable} to run
   * @return the spawned {@link Thread}
   */
  public static Thread async(Runnable runner) {
    Thread t = new Thread(runner);
    t.start();
    return t;
  }

  /**
   * Returns a new parameter string suitable for sending to a UDP server.
   * 
   * @param name
   *          name of the parameter
   * @param value
   *          value of the parameter
   * @return a new parameter string suitable for sending to a UDP server.
   */
  public static String newParameter(String name, String value) {
    if (name == null) return null;
    StringBuffer sb = new StringBuffer();
    sb.append(name);
    if (value != null) {
      sb.append("=");
      sb.append(value);
    }
    return sb.toString();
  }

}